package com.manxi.mapper;

import com.github.pagehelper.Page;
import com.manxi.annotation.AutoFill;
import com.manxi.dto.SetmealPageQueryDTO;
import com.manxi.entity.Setmeal;
import com.manxi.enumeration.OperationType;
import com.manxi.vo.DishItemVO;
import com.manxi.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetmealMapper {
    /**
     * 根据分类id查询套餐的数量
     *
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Select("<script> " +
            "select s.* , c.name as categoryName from setmeal s left outer join category c on s.category_id = c.id " +
            "where 1 = 1 " +
            "<if test='name != null'>AND s.name like CONCAT('%',#{name},'%') </if> " +
            "<if test='categoryId != null'>AND s.category_id = #{categoryId} </if> " +
            "<if test='status != null'>AND s.status = #{status} </if> " +
            "order by s.create_time desc " +
            "</script>")
    Page<SetmealVO> PageQuery(SetmealPageQueryDTO setmealPageQueryDTO);

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Select("select * from setmeal where id = #{id}")
    Setmeal getById(Long id);

    /**
     * 新增套餐
     *
     * @param setmeal
     */
    @Insert("<script> " +
            "insert into setmeal (category_id,name,price,status,description,image,create_time,update_time,create_user,update_user) values " +
            "(#{categoryId},#{name},#{price},#{status},#{description},#{image},#{createTime},#{updateTime},#{createUser},#{updateUser}) " +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @AutoFill(value = OperationType.INSERT)
    void insert(Setmeal setmeal);

    /**
     * 修改套餐信息
     *
     * @param setmeal
     */
    @Update("<script> " +
            "update setmeal set " +
            "<if test = 'categoryId != null'>category_id = #{categoryId}, </if>" +
            "<if test = 'name != null'>name = #{name}, </if>" +
            "<if test = 'price != null'>price = #{price}, </if>" +
            "<if test = 'description != null'>description = #{description}, </if>" +
            "<if test = 'image != null'>image = #{image}, </if>" +
            "<if test = 'updateTime != null'>update_time = #{updateTime}, </if>" +
            "<if test = 'updateUser != null'>update_user = #{updateUser}, </if>" +
            "<if test = 'count != null'> count = #{count} </if>" +
            "<if test = 'status != null'>status = #{status} </if>" +
            "where id = #{id} " +
            "</script>")
    @AutoFill(value = OperationType.UPDATE)
    void update(Setmeal setmeal);

    /**
     * 根据id删除套餐
     *
     * @param id
     */
    @Delete("delete from setmeal where id = #{id}")
    void deleteBatch(Long id);

    /**
     * 根据分类id查询套餐
     *
     * @param setmeal
     * @return
     */
    @Select("<script> " +
            "select * from setmeal " +
            "where 1= 1 " +
            "<if test='name != null'>AND name like CONCAT('%',#{name},'%') </if> " +
            "<if test='categoryId != null'>AND category_id = #{categoryId} </if> " +
            "<if test='status != null'>AND status = #{status} </if> " +
            "</script>")
    List<Setmeal> list(Setmeal setmeal);


    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param setmealId
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);


    /**
     * 查询套餐状态数量
     *
     * @param status
     * @return
     */
    @Select("select count(id) from setmeal where status = #{status}")
    Integer countStatus(Integer status);

}
